import axios from 'axios'
import type {AxiosResponse} from 'axios'
import 'element-plus/es/components/message/style/css'
import 'element-plus/es/components/notification/style/css'
import {ElMessage, ElNotification} from 'element-plus'
// import '../../public/config'

axios.defaults.withCredentials = true
export class Server {
    static host = (window as any).urlConfig.webApiURL
    static fileBaseURL = Server.host + '/file/'
    static axios = axios.create({baseURL: Server.host})

    static async get(url: string, params?: any, showMsg: boolean = false): Promise<AxiosResponse<any, any>> {
        return new Promise(function (resolve, reject): any {
            Server.axios({url, method: 'get', params}).then(res => {
                if (showMsg) Server.checkShowMessage(res)
                resolve(res)
            }).catch(reject)
        })
    }
    static async post(url: string, data?: any, showMsg: boolean = false): Promise<AxiosResponse<any, any>> {
        return new Promise(function (resolve, reject): any {
            axios.post(Server.host + url, data, ).then(res => {
                if (showMsg) Server.checkShowMessage(res)
                resolve(res)
            }).catch(reject)
        })
    }
    static async checkShowMessage(res: any) {
        if (res.data) {
            let data = res.data
            ElMessage({
                showClose: true,
                message: data.message,
                type: data.code != 0 ? 'warning' : 'success',
            })
        }
    }
    static async showMessage(str: string, type = 'warning' as "success" | "warning" | "info" | "error") {
        ElMessage({
            showClose: true,
            message: str,
            type
        })
    }
}
Server.axios.defaults.withCredentials = true
Server.axios.defaults.headers.common['Authorization'] = ''

